home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / TransSkel Pascal 2.5 / TransEdit / DumbEdit / DumbEdit.p < prev    next >
Encoding:
Text File  |  1994-12-05  |  10.6 KB  |  465 lines  |  [TEXT/PJMM]

  1. {    DumbEdit - Multiple-window TransEdit Demonstration.}
  2.  
  3. {    The project should include DumbEdit.p (this file), TransEdit.p,}
  4. {    FakeAlert.p, TransSkel.p (or a library made from TransSkel.p)}
  5. {    and RunTime.lib and Interface.lib.}
  6.  
  7. {    28 October 1986        Paul DuBois}
  8.  
  9. {     10 January 1987 Ported to Lightspeed Pascal by Owen Hartnett    }
  10. {    Ωhm Software Co. 163 Richard Drive, Tiverton, RI 02878            }
  11. {    30 December 1987 OH version 1.03 changes            }
  12. {    5 December 1988 OH version 2.00 changes            }
  13.  
  14.  
  15. program DumbEdit;
  16.  
  17.     uses
  18. {$IFC UNDEFINED THINK_PASCAL}
  19.         Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf, 
  20. {$ENDC}
  21.         TransEdit, TransSkel;
  22.  
  23.     const
  24.         maxSize = 8;        { no. font sizes made available }
  25.         hSize = 300;        { horiz, vert size of new windows }
  26.         vSize = 205;
  27.         aboutAlrt = 1000;
  28.  
  29.         { File menu item numbers }
  30.  
  31.         new = 1;                { begin new window }
  32.         open = 2;                { open existing file }
  33.         close = 3;                { close file }
  34.         save = 5;                { save file }
  35.         saveas = 6;            { save under another name }
  36.         saveCopy = 7;        { save a copy w/o switching file binding }
  37.         revert = 8;            { revert to version on disk }
  38.         quit = 10;
  39.  
  40.             { Edit menu item numbers }
  41.  
  42.         undo = 1;
  43.         cut = 3;
  44.         copy = 4;
  45.         paste = 5;
  46.         clear = 6;
  47.  
  48.         { Format menu item numbers }
  49.  
  50.         wordWrap = 1;
  51.         noWrap = 2;
  52.         leftJust = 4;
  53.         centerJust = 5;
  54.         rightJust = 6;
  55.  
  56.     var
  57.         lastFront: WindowPtr;    { keeps track of front window }
  58.         fileMenu, editMenu, fontMenu, sizeMenu, formatMenu: MenuHandle;
  59.  
  60.         sizes: array[0..maxSize] of integer;
  61.  
  62.         dummy: Boolean;
  63.  
  64. {    Uncheck all the items in a menu}
  65.  
  66.  
  67.     procedure UncheckMenu (theMenu: MenuHandle);
  68.         var
  69.             i, nItems: integer;
  70.  
  71.     begin
  72.         nItems := CountMItems(theMenu);
  73.  
  74.         for i := 1 to nItems do
  75.             begin
  76.                 CheckItem(theMenu, i, false);
  77.                 SetItemStyle(theMenu, i, []);
  78.             end;
  79.     end;
  80.  
  81. {    Set the Font, Size and Format menus so that the items corresponding}
  82. {    to the text characteristics of the window are checked.  If the}
  83. {    window isn't an edit window, dim all three menus.}
  84.  
  85.  
  86.     procedure SetTextMenus (drawBar: Boolean);
  87.  
  88.         var
  89.             theWind: WindowPtr;
  90.             wFontName, mFontName: str255;
  91.             i, nItems: integer;
  92.             te: TEHandle;
  93.  
  94.     begin
  95.         theWind := Frontwindow;
  96.         UncheckMenu(fontMenu);                { toss current check marks }
  97.         UncheckMenu(sizeMenu);
  98.         UncheckMenu(formatMenu);
  99.  
  100.         if not IsEWindow(theWind) then            { disable the menus }
  101.             begin
  102.                 DisableItem(fontMenu, 0);
  103.                 DisableItem(sizeMenu, 0);
  104.                 DisableItem(formatMenu, 0);
  105.             end
  106.         else
  107.             begin
  108.                 EnableItem(fontMenu, 0);
  109.                 EnableItem(sizeMenu, 0);
  110.                 EnableItem(formatMenu, 0);
  111.  
  112.                 te := GetEWindowTE(theWind);
  113.                 if te^^.crOnly < 0 then
  114.  
  115. {    Check appropriate word wrap item}
  116.  
  117.                     CheckItem(formatMenu, noWrap, true)
  118.                 else
  119.                     CheckItem(formatMenu, wordWrap, true);
  120.  
  121. {    Check appropriate justification item{}
  122.  
  123.                 case te^^.just of
  124.                     teJustLeft: 
  125.                         CheckItem(formatMenu, leftJust, true);
  126.                     teJustRight: 
  127.                         CheckItem(formatMenu, rightJust, true);
  128.                     teJustCenter: 
  129.                         CheckItem(formatMenu, centerjust, true);
  130.                     otherwise
  131.                 end;
  132.  
  133. {    Check appropriate font name item}
  134.  
  135.                 for i := 0 to maxSize - 1 do
  136.                     begin
  137.                         if te^^.txSize = sizes[i] then
  138.                             checkitem(sizeMenu, i + 1, true);
  139.                         if RealFont(te^^.txFont, sizes[i]) then
  140.                             SetItemStyle(sizeMenu, i + 1, [outline])
  141.                         else
  142.                             SetItemStyle(sizeMenu, i + 1, []);
  143.                         GetFontName(te^^.txFont, wFontName);
  144.                         nItems := CountMItems(fontMenu);
  145.                     end;
  146.                 for i := 1 to nItems - 1 do
  147.                     begin
  148.                         GetItem(fontmenu, i, mFontName);
  149.                         if EqualString(wFontName, mFontName, false, true) then
  150.                             CheckItem(fontMenu, i, true);
  151.                     end;
  152.                 if drawBar then
  153.                     DrawMenuBar;
  154.             end;
  155.     end;
  156.  
  157. {    Set File/Edit menu items according to type of front window.}
  158.  
  159. {    The general behavior is:}
  160.  
  161. {    New and Open always enabled, since a new edit window can always be}
  162. {    opened.}
  163.  
  164. {    Close enabled when an edit or DA window in front (i.e., when there's}
  165. {    a window at all).}
  166.  
  167. {    Save enabled for edit windows not bound to a file, and edit windows}
  168. {    bound to a file when they're dirty (typed into, Edit menu used to}
  169. {    do something to them).}
  170.  
  171. {    Save As and Save a Copy As enabled for edit windows.}
  172.  
  173. {    Revert enabled for edit windows bound to a file when they're dirty.}
  174.  
  175. {    Undo disabled when there's an edit window in front.}
  176.  
  177.     procedure SetNonTextmenus;
  178.  
  179.         var
  180.             theWind: WindowPtr;
  181.             theKind: integer;
  182.             thePeek: windowPeek;
  183.  
  184.     begin
  185.         DisableItem(fileMenu, close);    { assume no window at all }
  186.         DisableItem(fileMenu, save);
  187.         DisableItem(fileMenu, saveas);
  188.         DisableItem(fileMenu, savecopy);
  189.         DisableItem(fileMenu, revert);
  190.         EnableItem(editMenu, undo);
  191.  
  192.         theKind := 0;
  193.         theWind := FrontWindow;
  194.         thePeek := WindowPeek(theWind);
  195.         if theWind <> nil then
  196.             theKind := thePeek^.windowKind;
  197.         if theKind < 0 then                            { DA in front }
  198.             EnableItem(fileMenu, close)
  199.         else if IsEWindow(theWind) then            { edit window in front }
  200.             begin
  201.                 EnableItem(fileMenu, close);
  202.                 EnableItem(fileMenu, saveas);
  203.                 EnableItem(fileMenu, savecopy);
  204.                 if (GetEWindowFile(theWind, nil) = false) then    { not bound to file }
  205.                     EnableItem(fileMenu, save)
  206.                 else if IsEWindowDirty(theWind) then            { bound - is it dirty? }
  207.                     begin
  208.                         EnableItem(fileMenu, save);
  209.                         EnableItem(fileMenu, revert);
  210.                     end;
  211.                 DisableItem(editMenu, undo);
  212.             end;
  213.     end;
  214.  
  215. {    Background procedure.  Check front window, reset menus if it}
  216. {    changes.  The menu bar doesn't need redrawing by SetTextMenus}
  217. {    if the previous and current front window are either both edit}
  218. {    windows or both not edit windows.  This check eliminates some}
  219. {    needless menu flashing.}
  220.  
  221.     procedure CheckFront;
  222.  
  223.     begin
  224.         if FrontWindow <> lastFront then
  225.             begin
  226.                 SetNonTextMenus;
  227.                 if IsEWindow(FrontWindow) = IsEwindow(lastFront) then
  228.                     SetTextmenus(false)
  229.                 else
  230.                     SetTextmenus(true);
  231.                 lastFront := FrontWindow;
  232.             end;
  233.     end;
  234.  
  235. {    Got an activate or deactivate.  It doesn't matter which, really.}
  236. {    Set the text menus appropriately for the front window, and draw}
  237. {    the menu bar, as these menus might change state from enabled to}
  238. {    disabled or vice-versa.}
  239.  
  240.  
  241.     procedure Activate (active: Boolean);
  242.     begin
  243.         CheckFront;
  244.     end;
  245.  
  246. {    Got a keyclick in an edit window.}
  247.  
  248.  
  249.     procedure Key;
  250.     begin
  251.         SetNonTextMenus;
  252.     end;
  253.  
  254. {    Close selected from File menu, or close box of edit window}
  255. {    clicked.}
  256.  
  257.     procedure myClose;
  258.         var
  259.             theWind: WindowPtr;
  260.             ignore: Boolean;
  261.  
  262.     begin
  263.         GetPort(theWind);
  264.         ignore := EWindowClose(theWind);
  265.         CheckFront;
  266.     end;
  267.  
  268.     procedure MakeWind (bindToFile: Boolean);
  269.  
  270.         var
  271.             r: Rect;
  272.             windCount: integer;
  273.             offset: integer;
  274.             ignore: WindowPtr;
  275.     begin
  276.         windCount := 0;
  277.         if FrontWindow = nil then
  278.             windCount := 0;
  279.         SetRect(r, 0, 0, hSize, vSize);
  280.         windCount := windCount + 1;
  281.         offset := 50 + (25 * (windCount mod 4));
  282.         OffsetRect(r, offset, offset);
  283.         ignore := NewEWindow(r, '', true, WindowPtr(-1), true, longint(0), bindToFile);
  284.     end;
  285.  
  286. {    File menu handler}
  287.  
  288.  
  289.     procedure DoFileMenu (item: integer);
  290.  
  291.         var
  292.             theWind: WindowPtr;
  293.             mypeek: WindowPeek;
  294.             ignore: Boolean;
  295.     begin
  296.         theWind := FrontWindow;
  297.         case item of
  298.             new: 
  299.                 MakeWind(false);
  300.             open: 
  301.                 MakeWind(true);
  302.             close: 
  303.                 if ISEWindow(theWind) then
  304.                     ignore := EWindowClose(theWind)
  305.                 else
  306.                     begin
  307.                         mypeek := WindowPeek(theWind);
  308.                         CloseDeskAcc(mypeek^.windowKind);    { DA in front }
  309.                     end;
  310.             save: 
  311.                 ignore := EWindowSave(theWind);
  312.             saveas: 
  313.                 ignore := EWindowSaveAs(theWind);
  314.             revert: 
  315.                 ignore := EWindowRevert(theWind);
  316.             quit: 
  317.                 if ClobberEWindows = true then
  318.                     SkelWhoa;
  319.             otherwise
  320.         end;
  321.         SetNonTextMenus;
  322.     end;
  323.  
  324. {    Handle Font menu items}
  325.  
  326.  
  327.     procedure DoFontMenu (item: integer);
  328.  
  329.         var
  330.             font: integer;
  331.             te: TEHandle;
  332.             theWind: WindowPtr;
  333.             theFontName: Str255;
  334.  
  335.     begin
  336.         theWind := FrontWindow;
  337.         te := GetEWindowTE(theWind);
  338.         if te <> nil then                { not an edit window }
  339.             begin
  340.                 GetItem(fontMenu, item, theFontName);
  341.                 GetFNum(theFontName, font);
  342.                 SetEWindowSTyle(theWind, font, te^^.txSize, te^^.crOnly, te^^.just);
  343.                 SetTExtMenus(false);
  344.             end;
  345.     end;
  346.  
  347. {    Handle Size menu items }
  348.  
  349.     procedure DoSizeMenu (item: integer);
  350.  
  351.         var
  352.             te: TEHandle;
  353.             theWind: WindowPtr;
  354.     begin
  355.         theWind := FrontWindow;
  356.         te := GetEWindowTE(theWind);
  357.         if te <> nil then
  358.             begin
  359.                 SetEWindowStyle(theWind, te^^.txFont, sizes[item - 1], te^^.crOnly, te^^.just);
  360.                 SetTextMenus(false);
  361.             end;
  362.     end;
  363.  
  364. {    Handle Format menu items }
  365.  
  366.     procedure DoFormatMenu (item: integer);
  367.  
  368.         var
  369.             font, size, just, wrap: integer;
  370.             te: TEHandle;
  371.             theWind: WindowPtr;
  372.  
  373.     begin
  374.         theWind := FrontWindow;
  375.         te := GetEWindowTE(theWind);
  376.         if te <> nil then                {  an edit window }
  377.             begin
  378.                 font := te^^.txFont;
  379.                 size := te^^.txSize;
  380.                 just := te^^.just;
  381.                 wrap := te^^.crOnly;
  382.                 case item of
  383.                     wordWrap: 
  384.                         wrap := 0;
  385.                     noWrap: 
  386.                         wrap := -1;
  387.                     leftJust: 
  388.                         just := teJustLeft;
  389.                     centerJust: 
  390.                         just := teJustCenter;
  391.                     rightJust: 
  392.                         just := teJustRight;
  393.                     otherwise
  394.                 end;
  395.                 SetEWindowStyle(theWind, font, size, wrap, just);
  396.                 SetTextMenus(false);
  397.             end;
  398.     end;
  399.  
  400.     procedure DoAbout;
  401.         var
  402.             ignore: integer;
  403.     begin
  404.         ignore := Alert(aboutAlrt, nil);
  405.     end;
  406.  
  407. {    Initialize TransSkel, create menus and install handlers.}
  408.  
  409.  
  410. begin
  411.     lastFront := nil;
  412.     sizes[0] := 9;
  413.     sizes[1] := 10;
  414.     sizes[2] := 12;
  415.     sizes[3] := 14;
  416.     sizes[4] := 18;
  417.     sizes[5] := 20;
  418.     sizes[6] := 24;
  419.     sizes[7] := 48;
  420.     SkelInit(6, nil);
  421.     TransEditInit;
  422.     SkelApple('About DumbEdit…', @DoAbout);
  423.  
  424.     fileMenu := NewMenu(1000, 'File');
  425.     AppendMenu(fileMenu, 'New/N;Open…/O;Close/K;(-;Save/S;Save As…');
  426.     AppendMenu(fileMenu, 'Save a Copy As…;Revert/R;(-;Quit/Q');
  427.     dummy := SkelMenu(fileMenu, @DoFileMenu, nil, false);
  428.  
  429.     editMenu := NewMenu(1001, 'Edit');
  430.     AppendMenu(editMenu, 'Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear');
  431.     dummy := SkelMenu(editMenu, @EWindowEditOp, nil, false);
  432.  
  433.     fontMenu := NewMenu(1002, 'Font');
  434.     DisAbleItem(fontMenu, 0);
  435.     AddREsMenu(fontMenu, 'FONT');
  436.     dummy := SkelMenu(fontMenu, @DoFontMenu, nil, false);
  437.     sizeMenu := NewMenu(1003, 'Size');
  438.     DisableItem(sizeMenu, 0);
  439.     AppendMenu(sizemenu, '9 Point;10 Point;12 Point;14 Point');
  440.     AppendMenu(sizeMenu, '18 Point;20 Point;24 Point;48 Point');
  441.     dummy := SkelMenu(sizeMenu, @DoSizeMenu, nil, false);
  442.  
  443.     formatMenu := NewMenu(1004, 'Format');
  444.     DisableItem(formatMenu, 0);
  445.     AppendMenu(formatMenu, 'Word Wrap;No Word Wrap;(-;Left;Center;Right');
  446.     dummy := Skelmenu(formatMenu, @DoFormatMenu, nil, true);
  447.  
  448.     SetNonTextMenus;
  449.     SetTextMenus(true);
  450.  
  451. {    Do TransEdit-specific setup:  set creator for any files created,}
  452. {    set default text style and event notification procedures for}
  453. {    new windows.}
  454.  
  455.     SetEWindowCreator('DUMB');
  456.     SetEWindowStyle(nil, monaco, 9, 0, teJustLeft);
  457.     SetEWindowProcs(nil, @Key, @Activate, @myClose);
  458.  
  459. {    Process events until user quits,}
  460. {    then clean up and exit}
  461.  
  462.     SkelBackground(@CheckFront);
  463.     SkelMain;
  464.     SkelClobber;
  465. end.